﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using BabyFood.Data;
using BabyFood.Entities;

public partial class Admin_KhachHang_QLKhachHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }

    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        DataSet dsKhachHang = DataRepository.Provider.ExecuteDataSet("KhachHang_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsKhachHang.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsKhachHang.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;
    }
    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("QLKhachHang.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            string TenTruyCap = Common.StandardString(txtTenTruyCap.Text);
            if (H_CatalogId.Value.Length == 0 && DataRepository.KhachHangProvider.GetByTenTruyCap(TenTruyCap) != null)
            {
                lblStatus.Text = "Tên truy cập đã tồn tại, xin vui lòng chọn tên khác!";
                return;
            }
            KhachHang entKhachHang = new KhachHang();
            entKhachHang.Id = Convert.ToInt32(H_CatalogId.Value);
            entKhachHang.HoTen = Common.StandardString(txtTen.Text);
            string MatKhau = Common.StandardString(txtMatKhau.Text);
            string MaTruyCap = TenTruyCap + MatKhau;
            entKhachHang.TenTruyCap = TenTruyCap;
            entKhachHang.MaTruyCap = Common.EncryptMD5(MaTruyCap);
            entKhachHang.MatKhau = Common.EncryptMD5(MatKhau);
            entKhachHang.HopThu = Common.StandardString(txtHopThu.Text);
            entKhachHang.NgayCapNhat = Common.GetServerDateTime();

            bool updateStatus = false;
            try
            {
                if (entKhachHang.Id == 0)
                {
                    entKhachHang.Id = 0;
                    updateStatus = DataRepository.KhachHangProvider.Insert(entKhachHang);
                }
                else
                {
                    entKhachHang.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.KhachHangProvider.Update(entKhachHang);
                }
                if (!updateStatus)
                {
                    WebMessage.Show("Cập nhật Người dùng không thành công");
                }
                else
                {
                    H_CatalogId.Value = "0";
                    txtTen.Text = "";
                    txtMatKhau.Text = "";
                    txtTenTruyCap.Text = "";
                    txtHopThu.Text = "";
                    Bind_Data(Pager1.CurrentIndex);
                }
            }
            catch (Exception)
            {
                WebMessage.Show("Cập nhật Người dùng không thành công");
            }
        }
        catch (Exception)
        {
            WebMessage.Show("Cập nhật Người dùng không thành công");
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        txtTen.Text = "";
        txtMatKhau.Text = "";
        txtTenTruyCap.Text = "";
        lblStatus.Text = "";
        txtHopThu.Text = "";
        Bind_Data(Pager1.CurrentIndex);
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");
        }
    }
    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                KhachHang ent_KhachHang = DataRepository.KhachHangProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (ent_KhachHang != null)
                {
                    txtTen.Text = ent_KhachHang.HoTen;
                    txtHopThu.Text = ent_KhachHang.HopThu;
                    txtTenTruyCap.Text = ent_KhachHang.TenTruyCap;
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLKhachHang.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
            Bind_Data(Pager1.CurrentIndex);
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdKhachHang = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.KhachHangProvider.Delete(IdKhachHang);
                if (!deleteStatus)
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.KHACHHANG_XOA_THATBAI, true);
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.KHACHHANG_XOA_THANHCONG, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLKhachHang.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.KHACHHANG_XOA_THATBAI, true);
            }
        }
    }
    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data(Pager1.CurrentIndex);
        H_CatalogId.Value = "0";
        txtTen.Text = "";
        txtMatKhau.Text = "";
        txtTenTruyCap.Text = "";
        txtHopThu.Text = "";
    }
}
